//**************************************************************
// ****** FUNCTIONS FOR SPI COMMUNICATION *******
//**************************************************************
//Controller: ATmega8 (Clock: 8 Mhz-internal)
//Compiler: AVR-GCC
//Version : 2.0
//Author: 
//Date: 
//**************************************************************



//**************************************************
// ***** SOURCE FILE : SPI_routines.c ******
//**************************************************
#include <avr/io.h>
#include"usart.h"
#include "spi.h"

//SPI initialize for data flash
//clock rate: 125Khz
void spi_init(void)
{
/* Set MOSI and SCK , CS output, all others input */
DDRB  = (1<<PB0) |(1<<PB1) |(1<<PB2) |(0<<PB3);
//PORTB =0x00;
SPCR = 0x5C; //setup SPI: Master mode, MSB first, SCK phase low, SCK idle low
//SPCR = (1<<SPE) | (1<<MSTR) | (1<<CPHA) | (1<<CPOL)|(1<<SPR0)|(1<<SPR1);
SPSR = 0x00;

}


unsigned char SPI_transmit(unsigned char data)
{
SPSR =0x00;
// Start transmission
SPDR = data;

// Wait for transmission complete
while(!(SPSR & (1<<SPIF)));
  data = SPDR;
PORTG= ~PORTG;

return(data);

}


unsigned char SPI_receive(void)
{
//
unsigned char data;
// Wait for reception complete

SPDR = 0x00;
while(!(SPSR & (1<<SPIF)));

 data = SPDR;

return data;
}

//******** END ******

